package com.yuange.promethues.utils.common;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import javax.xml.soap.Text;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @作者：袁工
 * @时间：2021/12/30 13:21
 */
public class HttpUtils {

    /**
     * 发送GET请求
     * */
    public static String doGet(String url,Map<String,String> params) {
        try {
            HttpClient client = HttpClientBuilder.create().build();
            String paramsStr="";
            for (Map.Entry<String, String> entry : params.entrySet()) {
                String param=null;
                if(entry.getValue()!=null) {
                    param= URLEncoder.encode(entry.getValue(), "UTF-8");
                }
                paramsStr=paramsStr+entry.getKey()+"="+param+"&";
            }
            if(!paramsStr.equals("")) {
                paramsStr=paramsStr.substring(0, paramsStr.length()-1);
                paramsStr="?"+paramsStr;
            }
            HttpGet request = new HttpGet(url+paramsStr);
            HttpResponse response = client.execute(request);
            /**请求发送成功，并得到响应**/
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                /**读取服务器返回过来的json字符串数据**/
                String strResult = EntityUtils.toString(response.getEntity());
                return strResult;
            }
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
    * post请求（用于请求json格式的参数）
    * */
    public static String doPost(String url, String params) throws IOException {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);// 创建httpPost
        httpPost.setHeader("Accept", "application/json");
        httpPost.setHeader("Content-Type", "application/json");
        String charSet = "UTF-8";
        StringEntity entity = new StringEntity(params, charSet);
        httpPost.setEntity(entity);
        CloseableHttpResponse response = null;
        try {
            response = httpclient.execute(httpPost);
            StatusLine status = response.getStatusLine();
            int state = status.getStatusCode();
            if (state == HttpStatus.SC_OK) {
                HttpEntity responseEntity = response.getEntity();
                String jsonString = EntityUtils.toString(responseEntity);
                return jsonString;
            }
            else{
            }
        }
        finally {
            if (response != null) {
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            try {
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    /**
    * 发送GET请求
    * */
    public static String doGet(String url,String action,String port,Map<String,String> params) {
        try {
            HttpClient client = HttpClientBuilder.create().build();
            String paramsStr="";
            for (Map.Entry<String, String> entry : params.entrySet()) {
                String param=null;
                if(entry.getValue()!=null) {
                    param= URLEncoder.encode(entry.getValue(), "UTF-8");
                }
                paramsStr=paramsStr+entry.getKey()+"="+param+"&";
            }
            if(!paramsStr.equals("")) {
                paramsStr=paramsStr.substring(0, paramsStr.length()-1);
                paramsStr="?"+paramsStr;
            }
            HttpGet request = new HttpGet(url+":"+port+action+paramsStr);
            HttpResponse response = client.execute(request);
            /**请求发送成功，并得到响应**/
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                /**读取服务器返回过来的json字符串数据**/
                String strResult = EntityUtils.toString(response.getEntity());
                return strResult;
            }
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

}
